package com.nulldev.util.java.swing.extSwing;

import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import com.nulldev.util.image.ImageUtil;

public class ListEntryCellRenderer extends JLabel implements ListCellRenderer {
  private JLabel label;
 
  public Component getListCellRendererComponent(JList list, Object value,
                                                int index, boolean isSelected,
                                                boolean cellHasFocus) {
	  if (value instanceof ListEntry) {
		     ListEntry entry = (ListEntry) value;
		     
		     setText(value.toString());
		     if (entry.getIcon() != null) {
		    	 setIcon(entry.getIcon());
		     }
		     if (entry.getTooltip() != null && entry.getTooltip() != "") {
		    	 setToolTipText(entry.getTooltip());
		     }
		  
		     if (isSelected) {
		        setBackground(list.getSelectionBackground());
		        setForeground(list.getSelectionForeground());
		     }
		     else {
		        setBackground(list.getBackground());
		        setForeground(list.getForeground());
		     }
		 
		     setEnabled(list.isEnabled());
		     setFont(list.getFont());
		     setOpaque(true);
		 
		     return this;
	  }else {
		     setText(value.toString());
		     setIcon(new ImageIcon(ImageUtil.BLANK));
		  
		     if (isSelected) {
		        setBackground(list.getSelectionBackground());
		        setForeground(list.getSelectionForeground());
		     }
		     else {
		        setBackground(list.getBackground());
		        setForeground(list.getForeground());
		     }
		 
		     setEnabled(list.isEnabled());
		     setFont(list.getFont());
		     setOpaque(true);
		 
		     return this;
	  }
  }
}
